{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# 残差网络（ResNet）\n",
    ":label:`sec_resnet`\n",
    "\n",
    "随着我们设计越来越深的网络，深刻理解“新添加的层如何提升神经网络的性能”变得至关重要。更重要的是设计网络的能力，在这种网络中，添加层会使网络更具表现力，\n",
    "为了取得质的突破，我们需要一些数学基础知识。\n",
    "\n",
    "\n",
    "## 函数类\n",
    "\n",
    "首先，假设有一类特定的神经网络结构 $\\mathcal{F}$，它包括学习速率和其他超参数设置。\n",
    "对于所有 $f \\in \\mathcal{F}$，存在一些参数集（例如权重和偏置），这些参数可以通过在合适的数据集上进行训练而获得。\n",
    "现在假设 $f^*$ 是我们真正想要找到的函数，如果是 $f^* \\in \\mathcal{F}$，那我们可以轻而易举的训练得到它，但通常我们不会那么幸运。\n",
    "相反，我们将尝试找到一个函数 $f^*_\\mathcal{F}$，这是我们在 $\\mathcal{F}$ 中的最佳选择。\n",
    "例如，给定一个具有 $\\mathbf{X}$ 特性和 $\\mathbf{y}$ 标签的数据集，我们可以尝试通过解决以下优化问题来找到它：\n",
    "\n",
    "$$f^*_\\mathcal{F} := \\mathop{\\mathrm{argmin}}_f L(\\mathbf{X}, \\mathbf{y}, f) \\text{ subject to } f \\in \\mathcal{F}.$$\n",
    "\n",
    "那么，怎样得到更近似真正 $f^*$ 的函数呢？\n",
    "唯一合理的可能性是，我们需要设计一个更强大的结构 $\\mathcal{F}'$。\n",
    "换句话说，我们预计 $f^*_{\\mathcal{F}'}$ 比 $f^*_{\\mathcal{F}}$ “更近似”。\n",
    "然而，如果 $\\mathcal{F} \\not\\subseteq \\mathcal{F}'$，则无法保证新的体系“更近似”。\n",
    "事实上， $f^*_{\\mathcal{F}'}$ 可能更糟：\n",
    "如 :numref:`fig_functionclasses` 所示，对于非嵌套函数（non-nested function）类，较复杂的函数类并不总是向“真”函数 $f^*$ 靠拢（复杂度由 $\\mathcal{F}_1$ 向 $\\mathcal{F}_6$ 递增）。\n",
    "在 :numref:`fig_functionclasses` 的左边，虽然 $\\mathcal{F}_3$ 比 $\\mathcal{F}_1$ 更接近 $f^*$，但$\\mathcal{F}_6$ 却离的更远了。\n",
    "相反对于 :numref:`fig_functionclasses` 右侧的嵌套函数（nested function）类 $\\mathcal{F}_1 \\subseteq \\ldots \\subseteq \\mathcal{F}_6$，我们可以避免上述问题。\n",
    "\n",
    "![对于非嵌套函数类，较复杂（由较大区域表示）的函数类不能保证更接近“真”函数（ $f^*$ ）。这种现象在嵌套函数类中不会发生。](../img/functionclasses.svg)\n",
    ":label:`fig_functionclasses`\n",
    "\n",
    "因此，只有当较复杂的函数类包含较小的函数类时，我们才能确保提高它们的性能。\n",
    "对于深度神经网络，如果我们能将新添加的层训练成 *恒等映射*（identity function） $f(\\mathbf{x}) = \\mathbf{x}$ ，新模型和原模型将同样有效。\n",
    "同时，由于新模型可能得出更优的解来拟合训练数据集，因此添加层似乎更容易降低训练误差。\n",
    "\n",
    "针对这一问题，何恺明等人提出了*残差网络*（ResNet） :cite:`He.Zhang.Ren.ea.2016`。\n",
    "它在2015年的ImageNet图像识别挑战赛夺魁，并深刻影响了后来的深度神经网络的设计。\n",
    "残差网络的核心思想是：每个附加层都应该更容易地包含原始函数作为其元素之一。\n",
    "于是，*残差块* （residual blocks） 便诞生了，这个设计对如何建立深层神经网络产生了深远的影响。\n",
    "凭借它，ResNet 赢得了 2015 年 ImageNet 大规模视觉识别挑战赛。\n",
    "\n",
    "\n",
    "## (**残差块**)\n",
    "\n",
    "让我们聚焦于神经网络局部：如图 :numref:`fig_residual_block` 所示，假设我们的原始输入为 $x$ ，而希望学出的理想映射为 $f(\\mathbf{x})$ （作为 :numref:`fig_residual_block` 上方激活函数的输入）。\n",
    ":numref:`fig_residual_block` 左图虚线框中的部分需要直接拟合出该映射 $f(\\mathbf{x})$ ，而右图虚线框中的部分则需要拟合出残差映射 $f(\\mathbf{x}) - \\mathbf{x}$ 。\n",
    "残差映射在现实中往往更容易优化。\n",
    "以本节开头提到的恒等映射作为我们希望学出的理想映射 $f(\\mathbf{x})$ ，我们只需将 :numref:`fig_residual_block` 中右图虚线框内上方的加权运算（如仿射）的权重和偏置参数设成 0，那么 $f(\\mathbf{x})$ 即为恒等映射。\n",
    "实际中，当理想映射 $f(\\mathbf{x})$ 极接近于恒等映射时，残差映射也易于捕捉恒等映射的细微波动。\n",
    ":numref:`fig_residual_block` 右图是 ResNet 的基础结构-- *残差块*（residual block）。\n",
    "在残差块中，输入可通过跨层数据线路更快地向前传播。\n",
    "\n",
    "![一个正常块（左图）和一个残差块（右图）。](../img/residual-block.svg)\n",
    ":label:`fig_residual_block`\n",
    "\n",
    "ResNet 沿用了 VGG 完整的 $3\\times 3$ 卷积层设计。\n",
    "残差块里首先有 2 个有相同输出通道数的 $3\\times 3$ 卷积层。\n",
    "每个卷积层后接一个批量归一化层和 ReLU 激活函数。\n",
    "然后我们通过跨层数据通路，跳过这 2 个卷积运算，将输入直接加在最后的 ReLU 激活函数前。\n",
    "这样的设计要求 2 个卷积层的输出与输入形状一样，从而可以相加。\n",
    "如果想改变通道数，就需要引入一个额外的 $1\\times 1$ 卷积层来将输入变换成需要的形状后再做相加运算。\n",
    "残差块的实现如下：\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import paddle\n",
    "import paddle.nn as nn\n",
    "from paddle.nn import functional as F\n",
    "\n",
    "class Residual(nn.Layer):\n",
    "    def __init__(self, input_channels, num_channels, use_1x1conv=False,\n",
    "                 strides=1):\n",
    "        super(Residual, self).__init__()\n",
    "        self.conv1 = nn.Conv2D(input_channels, num_channels, kernel_size=3,\n",
    "                               padding=1, stride=strides)\n",
    "        self.conv2 = nn.Conv2D(num_channels, num_channels, kernel_size=3,\n",
    "                               padding=1)\n",
    "        if use_1x1conv:\n",
    "            self.conv3 = nn.Conv2D(input_channels, num_channels,\n",
    "                                   kernel_size=1, stride=strides)\n",
    "        else:\n",
    "            self.conv3 = None\n",
    "        self.bn1 = nn.BatchNorm2D(num_channels)\n",
    "        self.bn2 = nn.BatchNorm2D(num_channels)\n",
    "        self.relu = nn.ReLU()\n",
    "\n",
    "    def forward(self, X):\n",
    "        Y = F.relu(self.bn1(self.conv1(X)))\n",
    "        Y = self.bn2(self.conv2(Y))\n",
    "        if self.conv3:\n",
    "            X = self.conv3(X)\n",
    "        Y += X\n",
    "        return F.relu(Y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "如图 :numref:`fig_resnet_block` 所示，此代码生成两种类型的网络：\n",
    "一种是在 `use_1x1conv=False` 、应用 ReLU 非线性函数之前，将输入添加到输出。\n",
    "另一种是在 `use_1x1conv=True` 时，添加通过 $1 \\times 1$ 卷积调整通道和分辨率。\n",
    "\n",
    "![包含以及不包含 $1 \\times 1$ 卷积层的残差块。](../img/resnet-block.svg)\n",
    ":label:`fig_resnet_block`\n",
    "\n",
    "下面我们来查看[**输入和输出形状一致**]的情况。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[4, 3, 6, 6]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "blk = Residual(3, 3)\n",
    "X = paddle.rand([4, 3, 6, 6])\n",
    "Y = blk(X)\n",
    "Y.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "我们也可以在[**增加输出通道数的同时，减半输出的高和宽**]。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[4, 6, 3, 3]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "blk = Residual(3, 6, use_1x1conv=True, strides=2)\n",
    "blk(X).shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## [**ResNet模型**]\n",
    "\n",
    "ResNet 的前两层跟之前介绍的 GoogLeNet 中的一样：\n",
    "在输出通道数为 64、步幅为 2 的 $7 \\times 7$ 卷积层后，接步幅为 2 的 $3 \\times 3$ 的最大池化层。\n",
    "不同之处在于 ResNet 每个卷积层后增加了批量归一化层。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "b1 = nn.Sequential(nn.Conv2D(1, 64, kernel_size=7, stride=2, padding=3),\n",
    "                   nn.BatchNorm2D(64), nn.ReLU(),\n",
    "                   nn.MaxPool2D(kernel_size=3, stride=2, padding=1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "GoogLeNet 在后面接了 4 个由Inception块组成的模块。\n",
    "ResNet 则使用 4 个由残差块组成的模块，每个模块使用若干个同样输出通道数的残差块。\n",
    "第一个模块的通道数同输入通道数一致。\n",
    "由于之前已经使用了步幅为 2 的最大池化层，所以无须减小高和宽。\n",
    "之后的每个模块在第一个残差块里将上一个模块的通道数翻倍，并将高和宽减半。\n",
    "\n",
    "下面我们来实现这个模块。注意，我们对第一个模块做了特别处理。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def resnet_block(input_channels, num_channels, num_residuals,\n",
    "                 first_block=False):\n",
    "    blk = []\n",
    "    for i in range(num_residuals):\n",
    "        if i == 0 and not first_block:\n",
    "            blk.append(\n",
    "                Residual(input_channels, num_channels, use_1x1conv=True,\n",
    "                         strides=2))\n",
    "        else:\n",
    "            blk.append(Residual(num_channels, num_channels))\n",
    "    return blk"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "接着在 ResNet 加入所有残差块，这里每个模块使用 2 个残差块。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "b2 = nn.Sequential(*resnet_block(64, 64, 2, first_block=True))\n",
    "b3 = nn.Sequential(*resnet_block(64, 128, 2))\n",
    "b4 = nn.Sequential(*resnet_block(128, 256, 2))\n",
    "b5 = nn.Sequential(*resnet_block(256, 512, 2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "最后，与 GoogLeNet 一样，在 ResNet 中加入全局平均池化层，以及全连接层输出。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "ResNet = nn.Sequential(b1, b2, b3, b4, b5, nn.AdaptiveAvgPool2D((1, 1)),\n",
    "                    nn.Flatten(), nn.Linear(512, 10))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "每个模块有 4 个卷积层（不包括恒等映射的 $1\\times 1$ 卷积层）。\n",
    "加上第一个 $7\\times 7$ 卷积层和最后一个全连接层，共有 18 层。\n",
    "因此，这种模型通常被称为 ResNet-18。\n",
    "通过配置不同的通道数和模块里的残差块数可以得到不同的 ResNet 模型，例如更深的含 152 层的 ResNet-152。\n",
    "虽然 ResNet 的主体结构跟 GoogLeNet类似，但 ResNet 结构更简单，修改也更方便。这些因素都导致了 ResNet 迅速被广泛使用。\n",
    " :numref:`fig_resnet18` 描述了完整的 ResNet-18。\n",
    "\n",
    "![ResNet-18 架构](../img/resnet18.svg)\n",
    ":label:`fig_resnet18`\n",
    "\n",
    "在训练 ResNet 之前，让我们[**观察一下ResNet中不同模块的输入形状是如何变化的**]。\n",
    "在之前所有架构中，分辨率降低，通道数量增加，直到全局平均池化层聚集所有特征。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-------------------------------------------------------------------------------\n",
      "   Layer (type)         Input Shape          Output Shape         Param #    \n",
      "===============================================================================\n",
      "     Conv2D-6        [[1, 1, 224, 224]]   [1, 64, 112, 112]        3,200     \n",
      "   BatchNorm2D-5    [[1, 64, 112, 112]]   [1, 64, 112, 112]         256      \n",
      "      ReLU-3        [[1, 64, 112, 112]]   [1, 64, 112, 112]          0       \n",
      "    MaxPool2D-1     [[1, 64, 112, 112]]    [1, 64, 56, 56]           0       \n",
      "     Conv2D-7        [[1, 64, 56, 56]]     [1, 64, 56, 56]        36,928     \n",
      "   BatchNorm2D-6     [[1, 64, 56, 56]]     [1, 64, 56, 56]          256      \n",
      "     Conv2D-8        [[1, 64, 56, 56]]     [1, 64, 56, 56]        36,928     \n",
      "   BatchNorm2D-7     [[1, 64, 56, 56]]     [1, 64, 56, 56]          256      \n",
      "    Residual-3       [[1, 64, 56, 56]]     [1, 64, 56, 56]           0       \n",
      "     Conv2D-9        [[1, 64, 56, 56]]     [1, 64, 56, 56]        36,928     \n",
      "   BatchNorm2D-8     [[1, 64, 56, 56]]     [1, 64, 56, 56]          256      \n",
      "     Conv2D-10       [[1, 64, 56, 56]]     [1, 64, 56, 56]        36,928     \n",
      "   BatchNorm2D-9     [[1, 64, 56, 56]]     [1, 64, 56, 56]          256      \n",
      "    Residual-4       [[1, 64, 56, 56]]     [1, 64, 56, 56]           0       \n",
      "     Conv2D-11       [[1, 64, 56, 56]]     [1, 128, 28, 28]       73,856     \n",
      "  BatchNorm2D-10     [[1, 128, 28, 28]]    [1, 128, 28, 28]         512      \n",
      "     Conv2D-12       [[1, 128, 28, 28]]    [1, 128, 28, 28]       147,584    \n",
      "  BatchNorm2D-11     [[1, 128, 28, 28]]    [1, 128, 28, 28]         512      \n",
      "     Conv2D-13       [[1, 64, 56, 56]]     [1, 128, 28, 28]        8,320     \n",
      "    Residual-5       [[1, 64, 56, 56]]     [1, 128, 28, 28]          0       \n",
      "     Conv2D-14       [[1, 128, 28, 28]]    [1, 128, 28, 28]       147,584    \n",
      "  BatchNorm2D-12     [[1, 128, 28, 28]]    [1, 128, 28, 28]         512      \n",
      "     Conv2D-15       [[1, 128, 28, 28]]    [1, 128, 28, 28]       147,584    \n",
      "  BatchNorm2D-13     [[1, 128, 28, 28]]    [1, 128, 28, 28]         512      \n",
      "    Residual-6       [[1, 128, 28, 28]]    [1, 128, 28, 28]          0       \n",
      "     Conv2D-16       [[1, 128, 28, 28]]    [1, 256, 14, 14]       295,168    \n",
      "  BatchNorm2D-14     [[1, 256, 14, 14]]    [1, 256, 14, 14]        1,024     \n",
      "     Conv2D-17       [[1, 256, 14, 14]]    [1, 256, 14, 14]       590,080    \n",
      "  BatchNorm2D-15     [[1, 256, 14, 14]]    [1, 256, 14, 14]        1,024     \n",
      "     Conv2D-18       [[1, 128, 28, 28]]    [1, 256, 14, 14]       33,024     \n",
      "    Residual-7       [[1, 128, 28, 28]]    [1, 256, 14, 14]          0       \n",
      "     Conv2D-19       [[1, 256, 14, 14]]    [1, 256, 14, 14]       590,080    \n",
      "  BatchNorm2D-16     [[1, 256, 14, 14]]    [1, 256, 14, 14]        1,024     \n",
      "     Conv2D-20       [[1, 256, 14, 14]]    [1, 256, 14, 14]       590,080    \n",
      "  BatchNorm2D-17     [[1, 256, 14, 14]]    [1, 256, 14, 14]        1,024     \n",
      "    Residual-8       [[1, 256, 14, 14]]    [1, 256, 14, 14]          0       \n",
      "     Conv2D-21       [[1, 256, 14, 14]]     [1, 512, 7, 7]       1,180,160   \n",
      "  BatchNorm2D-18      [[1, 512, 7, 7]]      [1, 512, 7, 7]         2,048     \n",
      "     Conv2D-22        [[1, 512, 7, 7]]      [1, 512, 7, 7]       2,359,808   \n",
      "  BatchNorm2D-19      [[1, 512, 7, 7]]      [1, 512, 7, 7]         2,048     \n",
      "     Conv2D-23       [[1, 256, 14, 14]]     [1, 512, 7, 7]        131,584    \n",
      "    Residual-9       [[1, 256, 14, 14]]     [1, 512, 7, 7]           0       \n",
      "     Conv2D-24        [[1, 512, 7, 7]]      [1, 512, 7, 7]       2,359,808   \n",
      "  BatchNorm2D-20      [[1, 512, 7, 7]]      [1, 512, 7, 7]         2,048     \n",
      "     Conv2D-25        [[1, 512, 7, 7]]      [1, 512, 7, 7]       2,359,808   \n",
      "  BatchNorm2D-21      [[1, 512, 7, 7]]      [1, 512, 7, 7]         2,048     \n",
      "    Residual-10       [[1, 512, 7, 7]]      [1, 512, 7, 7]           0       \n",
      "AdaptiveAvgPool2D-1   [[1, 512, 7, 7]]      [1, 512, 1, 1]           0       \n",
      "     Flatten-1        [[1, 512, 1, 1]]         [1, 512]              0       \n",
      "     Linear-1            [[1, 512]]            [1, 10]             5,130     \n",
      "===============================================================================\n",
      "Total params: 11,186,186\n",
      "Trainable params: 11,170,570\n",
      "Non-trainable params: 15,616\n",
      "-------------------------------------------------------------------------------\n",
      "Input size (MB): 0.19\n",
      "Forward/backward pass size (MB): 49.96\n",
      "Params size (MB): 42.67\n",
      "Estimated Total Size (MB): 92.83\n",
      "-------------------------------------------------------------------------------\n",
      "\n",
      "{'total_params': 11186186, 'trainable_params': 11170570}\n"
     ]
    }
   ],
   "source": [
    "print(paddle.summary(ResNet, (1, 1, 224, 224)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## [**训练模型**]\n",
    "\n",
    "同之前一样，我们在 Fashion-MNIST 数据集上训练 ResNet。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The loss value printed in the log is the current step, and the metric is the average value of previous steps.\n",
      "Epoch 1/1\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/fluid/layers/utils.py:77: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working\n",
      "  return (isinstance(seq, collections.Sequence) and\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "step 200/235 - loss: 0.3582 - acc_top1: 0.6745 - acc_top5: 0.9493 - 79ms/step\n",
      "step 235/235 - loss: 0.3519 - acc_top1: 0.6971 - acc_top5: 0.9562 - 78ms/step\n",
      "Eval begin...\n",
      "step 40/40 - loss: 0.3591 - acc_top1: 0.8324 - acc_top5: 0.9966 - 45ms/step\n",
      "Eval samples: 10000\n"
     ]
    }
   ],
   "source": [
    "import paddle.vision.transforms as T\n",
    "from paddle.vision.datasets import FashionMNIST\n",
    "\n",
    "lr, num_epochs, batch_size = 0.01, 10, 256\n",
    "\n",
    "# 数据集处理\n",
    "transform = T.Compose([\n",
    "    T.Resize(96),\n",
    "    T.Transpose(),\n",
    "    T.Normalize([127.5], [127.5]),\n",
    "])\n",
    "# 数据集定义\n",
    "train_dataset = FashionMNIST(mode='train', transform=transform)\n",
    "val_dataset = FashionMNIST(mode='test', transform=transform)\n",
    "\n",
    "# 模型设置\n",
    "model = paddle.Model(ResNet)\n",
    "model.prepare(\n",
    "    paddle.optimizer.Adam(learning_rate=lr, parameters=model.parameters()),\n",
    "    paddle.nn.CrossEntropyLoss(),\n",
    "    paddle.metric.Accuracy(topk=(1, 5)))\n",
    "# 模型训练\n",
    "model.fit(train_dataset, val_dataset, epochs=num_epochs, batch_size=batch_size, log_freq=200)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## 小结\n",
    "\n",
    "* 学习嵌套函数（nested function）是训练神经网络的理想情况。在深层神经网络中，学习另一层作为恒等映射（identity function）较容易（尽管这是一个极端情况）。\n",
    "* 残差映射可以更容易地学习同一函数，例如将权重层中的参数近似为零。\n",
    "* 利用残差块（residual blocks）可以训练出一个有效的深层神经网络：输入可以通过层间的残余连接更快地向前传播。\n",
    "* 残差网络（ResNet）对随后的深层神经网络设计产生了深远影响，无论是卷积类网络还是全连接类网络。\n",
    "\n",
    "\n",
    "## 练习\n",
    "\n",
    "1. :numref:`fig_inception` 中的Inception块与残差块之间的主要区别是什么？在删除了Inception块中的一些路径之后，它们是如何相互关联的？\n",
    "1. 参考 ResNet 论文 :cite:`He.Zhang.Ren.ea.2016` 中的表 1，以实现不同的变体。\n",
    "1. 对于更深层次的网络，ResNet 引入了“bottleneck”架构来降低模型复杂性。请你试着去实现它。\n",
    "1. 在 ResNet 的后续版本中，作者将“卷积层、批量归一化层和激活层”结构更改为“批量归一化层、激活层和卷积层”结构。请你做这个改进。详见 :cite:`He.Zhang.Ren.ea.2016*1` 中的图 1。\n",
    "1. 为什么即使函数类是嵌套的，我们仍然要限制增加函数的复杂性呢？\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "[Discussions](https://discuss.d2l.ai/t/1877)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "PaddlePaddle 2.1.0 (Python 3.5)",
   "language": "python",
   "name": "py35-paddle1.2.0"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
